# -*- coding: UTF-8 -*-

from datetime import datetime
import sys
from sqlalchemy import and_,  func, or_
from typing import Dict, List, Any, Generator

from sqlalchemy import Boolean, Column, DateTime, Integer, String, Index, Table, ForeignKey, text, JSON, TEXT, distinct
from sqlalchemy.orm import Session, relationship
from common.database import Base
from common.database import SessionMixin
from common.utils import is_empty
from core.logger import logger



__all__ = ['NCLogAlarmNotice']

class NCLogAlarmNotice(Base, SessionMixin):
    '''
    日志告警通知表
    '''
    __tablename__ = "nc_log_alarm_notice"
    name = Column(String(256), nullable=False, index=True, comment='服务名称')
    # bigdata、app_server、database
    server_type = Column(String(32), nullable=False, index=True, comment='服务类型')
    # critical:严重(电话部门负责人), major:重要警告(电话告警处理人), warning:一般告警(短信告警处理人),
    # info:提示告警(企业微信告警处理人), nothing:(不告警)
    level = Column(String(16), nullable=True, default="info", index=True, comment='告警级别')
    notice_teams_uuid = Column(JSON, nullable=False, default=[], comment='通知小组')
    maintain_begin = Column(DateTime, nullable=True, comment='维护开始时间')
    maintain_end = Column(DateTime, nullable=True, comment='维护结束时间')
    department = Column(String(128), nullable=False, index=True, comment='所属部门')
    update_at = Column(DateTime, nullable=False, default=datetime.now, onupdate=datetime.now, comment='更新时间')
    comment = Column(String(256), nullable=True, comment='日志告警通知说明')


    __table_args__ = (
        Index('idx_nc_log_alarm_notice_name', 'name', unique=True),
        {
            'mysql_engine': 'innodb',
            'mysql_charset': 'utf8mb4',
            'mysql_collate': 'utf8mb4_unicode_ci',
            'extend_existing': True,
            'mysql_row_format': 'dynamic',
        }
    )

    def __repr__(self):
        return f"<nc_log_alarm_notice='{self.name}'>"


    def object_to_dict(self, db: Session):
        result = {
            key: getattr(self, key)
            for key in self.__mapper__.c.keys()
        }

        return result



    @classmethod
    def get_object_info_for_block_global(cls, db: Session):
        """
        获取全局屏蔽的通知信息
        """
        object_info = db.query(cls).filter(
            cls.name == "BlockGlobalAlarm"
        ).to_dict_first()
 
        return object_info